<?php
/* TODO: Add code here */
require('config/globalconfig.php');
?>
<?php
include_once('classes/user_sphinx_search.php');
include_once('classes/editor.php');
include_once('classes/user_product.php');
include_once('classes/user_property.php');
include_once('classes/user_faq.php');
include_once('classes/user_section.php');
include_once('classes/user_catalogue.php');
include_once('classes/user_manufactory.php');
include_once('ckeditor/ckeditor.php');

//user_common::writeLog('insert',0);
$objProduct = new user_product($objConnection);
$objProperty = new user_property($objConnection);
$objFAQ = new user_faq($objConnection);
$objCat = new user_catalogue($objConnection);
$objSection = new user_section($objConnection);
$objManufactory = new user_manufactory($objConnection);

//$objProduct->insert('1','1','1','1');
if ($_pgR["act"]==user_product::ACT_ADD)
{
	if (user_common::isCLogin())
	{
		
		//lấy thông tin user
		$c_userInfo = $_SESSION[user_common::SES_C_USERINFO];
		$userID = $c_userInfo['user_id'];
		$date = user_common::getDateTime();
		$strProperty = urldecode($_pgR['property']);
		
		$strProperty = editor::rteSafe(html_entity_decode($strProperty,ENT_COMPAT ,'UTF-8' ));
		$arrProperty = explode(user_common::STRING_SEPARATE_1,$strProperty);
		//print_r($arrProperty);
		$strNameProperty = $arrProperty[0];
		$strValueProperty = $arrProperty[1];
		$strDes = $_pgR['des'];
		$strDes = editor::rteSafe(html_entity_decode($strDes,ENT_COMPAT ,'UTF-8' ));
		$strTree = $_pgR['tree'];
		$strHTMLTree = $_pgR['tree_html'];
		$strTree = editor::rteSafe(html_entity_decode($strTree,ENT_COMPAT ,'UTF-8' ));
		//$strHTMLTree = editor::rteSafe(html_entity_decode($strHTMLTree,ENT_COMPAT ,'UTF-8' ));
		
		$_pgR['name'] = editor::rteSafe(html_entity_decode($_pgR['name'],ENT_COMPAT ,'UTF-8' ));
		
		$resultID = $objProduct->insert($_pgR['manu'],$_pgR['cat'],$_pgR['name'],$strDes,$strTree,$strHTMLTree,$userID,$date);
		
		$objProperty->insertPropertyBatch($resultID,$_pgR['sect'],'1', $strNameProperty,$strValueProperty) ;
		
		//thêm phần bang cau hoi
		$strFAQ = urldecode($_pgR['faq']);
		$strFAQ = editor::rteSafe(html_entity_decode($strFAQ,ENT_COMPAT ,'UTF-8' ));
		$arrFAQ = explode(user_common::STRING_SEPARATE_1,$strFAQ);
		//print_r($arrProperty);
		$strQuestion = $arrFAQ[0];
		$strAnswer = $arrFAQ[1];
		
		$objFAQ->insertFaqBatch($resultID,'1', $strQuestion,$strAnswer) ;
		if ($resultID)
		{
			
			$objSphinx = new user_sphinx_search($objConnection,'t_product');
			
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			$objSphinx = new user_sphinx_search($objConnection,'t_property');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			
			$objSphinx = new user_sphinx_search($objConnection,'t_faq');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			
			
			$arrHeader = user_common::getMessageHeaderArr($banCode);//$banCode
			//echo $strHeaderMessage."1".consts::SEP_RESPONSE.$strResultHTML.consts::SEP_RETURN.$_pgR["ontop"];
			echo user_common::convertToXML(
					$arrHeader, array("rs", "inf"), 
					array(1, $result ), 
					array( 0, 1 )
					);
			return;
		}
	}
	else
	{
		echo user_common::convertToXML($arrHeader, array("rs",'info'), array(0,user_common::STRING_REQUIRE_LOGIN), array(0,1));
	}
	return;
}
if ($_pgR["act"]==user_product::ACT_UPDATE)
{
	if (user_common::isCLogin())
	{
		//lấy thông tin user
		$c_userInfo = $_SESSION[user_common::SES_C_USERINFO];
		$userID = $c_userInfo['user_id'];
		$date = user_common::getDateTime();
		
		$intID = $_pgR['id'];
		
		
		$strDes = $_pgR['des'];
		$strDes = editor::rteSafe(html_entity_decode($strDes,ENT_COMPAT ,'UTF-8' ));
		$strTree = $_pgR['tree'];
		$strTree = editor::rteSafe(html_entity_decode($strTree,ENT_COMPAT ,'UTF-8' ));
		$strHTMLTree = $_pgR['tree_html'];
		//$strHTMLTree = editor::rteSafe(html_entity_decode($strHTMLTree,ENT_COMPAT ,'UTF-8' ));
		
		$m_image = $intID;
		//đổi tên file image lại. Trước đó upload lên chỉ lưu tên tạm
		if(file_exists(user_product::FOLDER_IMAGE_PRODUCT.user_product::FILE_TEMP_UPLOAD))
		{
			rename(user_product::FOLDER_IMAGE_PRODUCT.user_product::FILE_TEMP_UPLOAD, user_product::FOLDER_IMAGE_PRODUCT.$m_image);
		}
		
		$_pgR['name'] = editor::rteSafe(html_entity_decode($_pgR['name'],ENT_COMPAT ,'UTF-8' ));
		$resultID = $objProduct->update($intID, $_pgR['manu'],$_pgR['cat'],$_pgR['name'],$strDes,$strTree,$strHTMLTree,$userID,$date,$m_image);
		
		$objFAQ->deleteFAQByProduct($intID,1);
		$objProperty->deletePropertyByProduct($intID,1);
		
		$strProperty = urldecode($_pgR['property']);
		$strProperty = editor::rteSafe(html_entity_decode($strProperty,ENT_COMPAT ,'UTF-8' ));
		
		$arrProperty = explode(user_common::STRING_SEPARATE_1,$strProperty);
		//print_r($arrProperty);
		$strNameProperty = $arrProperty[0];
		$strValueProperty = $arrProperty[1];
		$objProperty->insertPropertyBatch($resultID,$_pgR['sect'],'1', $strNameProperty,$strValueProperty) ;
		
		//thêm phần bang cau hoi
		$strFAQ = urldecode($_pgR['faq']);
		$strFAQ = editor::rteSafe(html_entity_decode($strFAQ,ENT_COMPAT ,'UTF-8' ));
		
		$arrFAQ = explode(user_common::STRING_SEPARATE_1,$strFAQ);
		//print_r($arrProperty);
		$strQuestion = $arrFAQ[0];
		$strAnswer = $arrFAQ[1];
		
		$objFAQ->insertFaqBatch($resultID,'1', $strQuestion,$strAnswer) ;
		
		if ($resultID)
		{
			
			$objSphinx = new user_sphinx_search($objConnection,'t_product');
			
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			$objSphinx = new user_sphinx_search($objConnection,'t_property');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			
			$objSphinx = new user_sphinx_search($objConnection,'t_faq');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			
			
			$arrHeader = user_common::getMessageHeaderArr($banCode);//$banCode
			//echo $strHeaderMessage."1".consts::SEP_RESPONSE.$strResultHTML.consts::SEP_RETURN.$_pgR["ontop"];
			echo user_common::convertToXML(
					$arrHeader, array("rs", "inf"), 
					array(1, $result ), 
					array( 0, 1 )
					);
			return;
		}
	}
	else
	{
		echo user_common::convertToXML($arrHeader, array("rs",'info'), array(0,user_common::STRING_REQUIRE_LOGIN), array(0,1));
	}
	return;
}
elseif($_pgR['act'] == user_product::ACT_CHANGE_PAGE)
{
	$intPage = $_pgR['p'];
	
	$outPutHTML =  $objProduct->getListProduct($intPage);
	echo user_common::convertToXML($strMessageHeader, array('rs','inf'), array(1,$outPutHTML),array(0,1));
	return ;
}
elseif($_pgR['act'] == user_product::ACT_SHOW_EDIT)
{
	
	$product_id = $_pgR['id'];
	$arrProduct =  $objProduct->getProductByID($product_id);
	$name = $arrProduct['name'];
	$image = '<img border="0" src="/images/products/'.$arrProduct['image'].' " width="300px" height="300px">'
	
	;
	$manu = $arrProduct['manu_id'];
	
	$cat = $arrProduct['cat_id'];
	$arrCat = $objCat->getCatByID($cat);
	$sect = $arrCat['section_id'];
	$description = $arrProduct['description'];
	$tree = $arrProduct['tree_function'];
	$treeHTML = $arrProduct['tree_html'];
	$arrCats = $objCat->getAllCatBySection($sect);
	$strCat = '<option value="0">--Chọn nhóm--</option>';
	foreach($arrCats as $item)
	{
		$strCat .= '<option value="'.$item['cat_id'].'">'.$item['name'].'</option>';
	}
	$arrFAQ = $objFAQ->getFaqByProduct($product_id,1);
	$strFAQ ="";
	foreach($arrFAQ as $item)
	{
		$strFAQ.= $item['question'].user_common::STRING_SEPARATE.$item['answer'].user_common::STRING_SEPARATE_1;
	}
	$arrProperty = $objProperty->getPropertyByProduct($product_id,1);
	$strProperty ="";
	foreach($arrProperty as $item)
	{
		$strProperty.= $item['name'].user_common::STRING_SEPARATE.$item['value'].user_common::STRING_SEPARATE_1;
	}
	
	
	echo user_common::convertToXML($strMessageHeader, 
			array('rs','id','name','image','manu','cat','sect','des','tree','cat_html','tree_html','faq','property'), 
			array(1,$product_id,$name,$image,$manu,$cat,$sect,$description,$tree,$strCat,$treeHTML,$strFAQ,$strProperty),array(0,1,1,1,1,1,1,1,1,1,1,1));
	return ;
}
// echo 'callcenter';
// user_common::writeLog("anc",1);

//xoa file image product
$filename = "images/products/name";
if (file_exists($filename)) 
{
	unlink($filename);
	
}
$arrManu = $objManufactory->getAllManufactory();
$arrSection = $objSection->getAllSection();

$strHTMLManu = $objManufactory->outputHTMLOptionManufactory($arrManu);
$strHTMLSection = $objSection->outputHTMLSection($arrSection);

include_once('include/_header.inc');
include_once('include/_menu.inc');
?>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('user_admin_product.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('user_section.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('user_catalogue.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('ajaxupload.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('editor.js');?>"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<style>
	.ui-button { margin-left: -1px; }
	.ui-button-icon-only .ui-button-text { padding: 0.35em; } 
	.ui-autocomplete-input { margin: 0; padding: 0.48em 0 0.47em 0.45em; }
	</style>
<script>
	
	$(function() {
		$( "#combobox" ).combobox();
		$( "#toggle" ).click(function() {
			$( "#combobox" ).toggle();
		});
	});
	$(function() {
		$( "#combobox2" ).combobox();
		$( "#toggle" ).click(function() {
			$( "#combobox2" ).toggle();
		});
	});
	
	</script>
	
<!--Begin Form Input -->
<input type="hidden" id="adddocmode" name="adddocmode" value="1<?php //echo $intMode;?>" />
<input type="hidden" id="txtPage" name="txtPage" value="<?php echo $_pgR["p"]?$intPage:1;?>" />
<input type="hidden" id="txtID" name="txtID" value="" />
 <center>
<br><h2 align="center">Quản lý sản phẩm</h2>
		<div class="input-field-border input-field-content" >
				<div id="lgTitle" class="div_admin_group_title" style="">
				<span style="cursor:default; font-family:inherit" id='status-add' name='status-add'>Add Mode</span></div>
				<div class="div_admin_group_content_inside" style="width: 100%; top: -20px;">
				  <table id="tblPopUp" style="width: 100%;" border="0" cellpadding="2" cellspacing="0">
				  <tbody>
									  
				  <tr>
					<td width="110"><span style="cursor:default; font-family:inherit">Tên sản phẩm </span></td>
					<td width="567"><input id="txtName" name="txtName" value="" style="width: 49.5%;"  maxlength="255" type="text"></td>
				  </tr>
				 <tr>
					<td width="110"><span style="cursor:default; font-family:inherit">Nhà sản xuất </span></td>
					<td width="567">
					<?php echo $strHTMLManu; ?>
						<!--select id="txtManu" name="txtManu" value="" style="width: 50%;"  >
						<option value="0">--Chọn nhà sản xuất--</option>
						<option value= "1">Nokia</option>
						<option value= "2">Samsum</option>
						<option value= "3">Sony</option>
						<option value= "4">Lenovo-IBM</option>
						<option value= "5">DELL</option>
						<option value= "6">Toshiba</option>
						<option value= "7">Apple</option>
						<option value= "8">Acer</option>
						</select-->
					</td>
				  </tr>
				<tr>
					<td width="110"><span style="cursor:default; font-family:inherit">Ngành hàng </span></td>
					<td width="500">
					<?php echo $strHTMLSection ?>
						<!--select id="txtSect" name="txtSect" value="" style="width: 50%;" onchange="_objAdminProduct.loadCatalogue()"  >
						<option value="0">--Chọn ngành hàng--</option>
						<option value= "1">Điện thoại</option>
						<option value= "2">Máy lạnh</option>
						</select-->
					</td>
				  </tr>
				 <tr>
					<td width="110"><span style="cursor:default; font-family:inherit">Nhóm hàng </span></td>
					<td width="567">
						<div id="content-cat">
							<select id="txtCat" name="txtCat" value="" style="width: 50%;" disabled="disabled" >
								<option value="0">--Chọn Nhóm hàng--</option>
								<option value= "1">Điện thoại thông minh</option>
								<option value= "2">Điện thoại phổ thông</option>
							</select>
						</div>
					</td>
				  </tr>
				  <tr >
					<td width="110"><label for="file_image" style="cursor:default; font-family:inherit">Hình ảnh</label></td>
					<td >
					<form action="ajaxupload.php?filename=name&amp;maxSize=9999999999&amp;maxW=800&amp;relPath=images/products/&amp;colorR=255&amp;colorG=255&amp;colorB=255&amp;maxH=800" method="post">
							<p><label for="name" >From Computer:</label><input type="file" id="name" name="name" onchange="ajaxUpload(this.form,'ajaxupload.php?filename=name&amp;maxSize=9999999999&amp;maxW=800&amp;relPath=images/products/&amp;colorR=255&amp;colorG=255&amp;colorB=255&amp;maxH=800','upload_area','File Uploading Please Wait...&lt;br /&gt;&lt;img src=\'images/loader_light_blue.gif\' width=\'128\' height=\'15\' border=\'0\' /&gt;','&lt;img src=\'images/error.gif\' width=\'16\' height=\'16\' border=\'0\' /&gt; Error in Upload, check settings and path info in source code.'); return false;" /></p>
						</form>
						
						
					<label for="urlImage" >From URL:</label><input id='urlImage' type="textbox" style="width: 30%;" onFocus="this.select()"/> <input type="button"  class="btn btn-oliver" value="Upload" onclick="_objCommon.uploadFromURL('urlImage');" />
					<div id="upload_area">
						<img border="0" src="/images/products/name" width="300px" height="300px">
						</div>
					<!--input id="file_image" name="file_image" value="" style="clear:both; vertical-align:super;width: 30%;margin-top:5px;padding-top:5px" maxlength="50" type="file">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
                    <img src="images/products/asusK100.jpg" width="30" height="30" style="margin-top:0px;padding-top:0px" /-->
                    </td>
				  </tr>
				 <tr >
					<td width="110" style="cursor:default; font-family:inherit;vertical-align:top;"><label for="txtDescription" >Mô tả</label></td>
					<td >
						<textarea id='txtDescription' name='txtDescription' style="width: 50%;"></textarea>
                    </td>
				  </tr>
				 <tr >
					<td width="110"><label for="tree_property" style="cursor:default; font-family:inherit">Cấu trúc chức năng</label></td>
					<td >
						<span id="tree_html">
							<select id="tree_property" name="tree_property" value="" style="width: 20%;"  >
						</select>
						</span>
						<input type="textbox" id="tree_property_input" class="" onFocus="this.select()"/>
						<input type="button"  class="btn btn-oliver"  class="" onclick="_objCommon.insertOptionTree('tree_property_input','tree_property');" value="Add"/>
						<input type="button"  class="btn btn-oliver"  class="" value="Delete" onclick=" _objCommon.removeOptionSelected('tree_property');"/>
						<input type="button"  class="btn btn-oliver"  class="" value="Clear" onclick=" _objCommon.clearAllOption('tree_property');"/>
						<input type="button"  class="btn btn-oliver"  class="" value="View" onclick="_objCommon.viewTree('tree_property');"/>
                    </td>
				  </tr>
				
				</tbody></table>
				</div>
	
	<div id="tabs">
	<ul>
		<li><a href="#tabs-property">Tính Năng</a></li>
		<li><a href="#tabs-faq">Hỏi Đáp</a></li>
		
	</ul>
	<div id="tabs-property">
		<div id="zone-properties">
			<!--div class="e-property">
				<select id="combobox" class="property-combobox">
					<option value="0">--Chọn hoặc gõ--</option>
					<option>Chụp hình</option>
					<option>Quay phim</option>
					<option>GPRS</option>
				</select><input type="textbox" id="combobox_input" class="property-input"/>
			</div>
			<div class="e-property">
				<select id="combobox2"   class="property-combobox">
					<option value="0">--Chọn hoặc gõ--</option>
					<option value="3g">3Gs</option>
					<option value="ch2">Chụp hình 2</option>
					<option value="qp2">Quay phim 2</option>
					<option value="GP">GPRS</option>
				</select><input type="textbox" id="combobox2_input" class="property-input"/>
			</div-->
		</div>
		<div class='control'>
		<input type='button' id='addSelect' value='add' onClick='_objAdminProduct.addElementProperty("zone-properties")' />
	</div>
	</div>
	<div id="tabs-faq">
		<div id='zone-faq'>
			<div class='e-faq'>
				<h2>Câu hỏi thứ 1:</h2>
				<textarea class="question-input" style="width:100%"></textarea>
				<textarea class="answer-input mce_advanced" id="cmt1" name="cmt1" class="" ></textarea>
				<script type="text/javascript">
					CKEDITOR.replace( 'cmt1',{
					filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
					filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?type=Images',
					filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?type=Flash',
					filebrowserUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
					filebrowserImageUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
					filebrowserFlashUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
				}
						 );
					//	CKEDITOR.replace( 'cmt'1,{			filebrowserBrowseUrl : '/ckfinder/ckfinder.html',			filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?type=Images',			filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?type=Flash',			filebrowserUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',			filebrowserImageUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',			filebrowserFlashUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'});
				</script>
			
			</div>
			
		</div>
		<div class='control'>
		<input type='button' id='addSelect' value='add' onClick='_objAdminProduct.addElementfaq("zone-faq")' />
	</div>
	</div>
	
</div>

				<div class="div_admin_group_content_inside" style="margin: 4px; display: block;" align="center">		
				  <input id="btnOK" value="OK"  style="width: 50px;" onClick="_objAdminProduct.btnSave_OnClick()" type="button" class="btn btn-oliver" > &nbsp;&nbsp;&nbsp;
				  <input id="btnClose" value="Cancel" align="center" style="width: 55px;" onClick="_objAdminProduct.showAddMode()" type="button"  class="btn btn-oliver">  
			  </div>					
		</div>	
	
		</center>
<!--End Form Input -->
   <div  id="content-admin" >
                    <div align="center">
	                    <h2>Danh sách</h2>									
					</div>
					<div id="list-content" style="padding:10px">
						<?php echo $objProduct->getListProduct(1) ?>					
						</div>
	</div>

<?php 
//footer
include_once('include/_footer.inc');
?>